package com.clusterswisdom.horn.api.tmall.action.alipay;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.HornTmcClient;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.clusterswisdom.horn.api.tmall.action.alipay.free.FreeUtil;
import com.clusterswisdom.horn.api.tmall.action.alipay.free.TmallTrade;
import com.taobao.api.domain.AlipayRecord;
import com.taobao.api.response.AlipayUserAccountreportGetResponse;
import com.taobao.api.response.TradeGetResponse;

public class TestOfUserTradeAction extends AbstractTmallAction implements HornAction{
	private Logger log = LoggerFactory.getLogger(HornTmcClient.class);
	
	public HornResponse execute(ApiConfig config, JSONObject param)
			throws Exception {
		AlipayUserAccountreportGetResponse response = (AlipayUserAccountreportGetResponse)TmallInstance.getInstance().executeRequest(param, "alipay", "testAlipayRecord");
		
		List<AlipayRecord> alipayRecords = response.getAlipayRecords();
		//List<TmallTrade> massage = (List<TmallTrade>) getEbizNeeded(alipayRecords);
		JSONObject object = new JSONObject();
		int i = 1;
		if(alipayRecords == null){
			
			return new HornResponse(true, "没有账单数据");
			
		}
		for(TmallTrade tmallTrade:getEbizNeeded(alipayRecords)){
			
			object.put(i++ +"", tmallTrade.toString());
			
			
		}
		
		return new HornResponse(true, object);
	}
	/**
	 * 获取ebiz所需要的数据
	 * @param alipayRecords
	 * @return
	 * @throws Exception 
	 */
	public Collection<TmallTrade> getEbizNeeded(List<AlipayRecord> alipayRecords) throws Exception{
		//torderid(订单号)totalMoney（应收款项）countMoney（实收款项）feeMoney(费用)differFee（差额）
		HashMap<String,TmallTrade> tmallTrades = new HashMap<String, TmallTrade>();
		HashMap<String,Double> tmallFrees = new HashMap<String, Double>();
		for(AlipayRecord alipayrecord : alipayRecords){
			//如果子业务类型为transfer_01则写入到订单的表中，否则写入费用表中
			if(alipayrecord.getBusinessType().equals("transfer_01")){
				TmallTrade tmallTrade = new TmallTrade(); 
				tmallTrade.setCountMoney(Double.parseDouble(alipayrecord.getInAmount()));
				tmallTrade.setTorderId(FreeUtil.getTradId(alipayrecord.getMerchantOrderNo()));
				
				tmallTrades.put(FreeUtil.getTradId(alipayrecord.getMerchantOrderNo()), tmallTrade);
			}else{
				String feeId = FreeUtil.getTradId(alipayrecord.getMemo());
				if(tmallFrees.get(feeId) == null){
					tmallFrees.put(feeId, Double.parseDouble(alipayrecord.getOutAmount()));
					
				}else{
					tmallFrees.put(feeId,tmallFrees.get(feeId)+Double.parseDouble(alipayrecord.getOutAmount()));
					
				}	
			}
		
		}
		for(String tradeId:tmallTrades.keySet()){
			tmallTrades.get(tradeId).setFeeMoney(tmallFrees.get(tradeId));
			JSONObject param = new JSONObject();
			log.debug("tradeId" + ":" + tradeId);
			param.put("tid", Long.parseLong(tradeId));
			TradeGetResponse response = (TradeGetResponse)TmallInstance.getInstance().executeRequest(param, "trade", "tradeGet");
			tmallTrades.get(tradeId).setTotalMoney(Double.parseDouble(response.getTrade().getPayment()));
		}
		
		return tmallTrades.values();
	}
}
